package org.alex.samples.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.alex.samples.model.ExamScoreVO;
import org.alex.samples.model.ExamSubjectVO;
import org.alex.samples.model.ExamTestVO;
import org.alex.samples.model.UsersVO;
import org.alex.samples.service.IStudentService;
import org.alex.samples.service.ISubjectService;
import org.alex.samples.service.ITestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;


@Controller
@RequestMapping(value="/student")
public class StudentController {
	
	@Autowired
	private IStudentService studentService;
	
	@Autowired
	private ISubjectService subjectService;
	
	@Autowired
	private ITestService testService;
	
	@RequestMapping(value = "/subjects")
	public String subjectsByStudent(HttpSession session, Model model){
		UsersVO userVO = (UsersVO) session.getAttribute("examUser");
		List<ExamSubjectVO> examSubjectList = studentService.getSubjectList(userVO);
		model.addAttribute("examSubjectList",examSubjectList);
		return "student/subjects";	
	}
	
	@ResponseBody
	@RequestMapping(value = "/showScore",method = RequestMethod.POST)
	public ExamScoreVO showScore(HttpSession session, Integer scoreId){
		ExamScoreVO vo = studentService.getScoreById(scoreId);
		return vo;
		
	}
	
	@RequestMapping(value = "/toTest/{subId}",method = RequestMethod.GET)
	public String showTest(HttpSession session, Model model,@PathVariable int subId){
		UsersVO userVO = (UsersVO) session.getAttribute("examUser");
		Map<Long, List<ExamTestVO>> TestBySuject = studentService.getTestBySuject(subId, userVO.getUserId());
		ExamSubjectVO s = subjectService.getSubjectById(subId);
		model.addAttribute("TestBySuject", TestBySuject);
		model.addAttribute("remainingTime", s.getTotalTime());
		model.addAttribute("subject", s);
		
		
		return "student/test";	
	}
	
	@RequestMapping(value = "/assignment",method = RequestMethod.POST)
	public String assignment(HttpServletRequest request, Model model, String userId, Integer subId, RedirectAttributes redirectAttributes){		
		Map<String, String[]> parameterMap = request.getParameterMap();
		Set<Entry<String, String[]>> entrySet = parameterMap.entrySet();
		List<Integer> answerIds = new ArrayList<Integer>();
		Map<String, String[]> testMap = new HashMap<String, String[]>();
		if(entrySet != null){
			Iterator<Entry<String, String[]>> it = entrySet.iterator();
			final String TEST_PREFIX = "test";
			while(it.hasNext()){
				Entry<String, String[]> entry = it.next();
				String key = entry.getKey();
				String[] splitArrayString = key.split("_");
				if(splitArrayString.length == 2){
					if(TEST_PREFIX.equals(splitArrayString[0])){
						answerIds.add(Integer.valueOf(splitArrayString[1]));
						testMap.put(splitArrayString[1], entry.getValue());
					}		
				}
			}
			
			ExamScoreVO score = testService.calculateScore(answerIds, testMap, userId, subId);
			ExamSubjectVO subjectVO = subjectService.getSubjectById(subId);
			redirectAttributes.addFlashAttribute("score", score);
			redirectAttributes.addFlashAttribute("subject", subjectVO);
			return "redirect:/student/subjects";
				
		}
		
		return null;
		
	}
	

}
